import { defineStore } from "pinia";
import { EditorSetting, getDefaultEditorSettingValue } from "@/domain/EditorSetting";
import Optional from "@/utils/Optional";
import LocalNameEnum from "@/enumeration/LocalNameEnum";

export const useEditorSettingStore = defineStore("editor-setting", () => {
  const editorSetting = useLocalStorage(LocalNameEnum.SETTING_EDITOR, getDefaultEditorSettingValue);
  const getSetting = () => editorSetting.value;
  const getFontSize = computed(() => Optional.ofNullable(editorSetting.value.fontSize).orElse(16));
  const getMinimap = computed(() => Optional.ofNullable(editorSetting.value.minimap).orElse(false));
  const getWordWrap = computed(() =>
    Optional.ofNullable(editorSetting.value.wordWrap).orElse("on")
  );
  const getRunColor = computed(() =>
    Optional.ofNullable(editorSetting.value.runColor).orElse("#0d7d6c")
  );

  const save = async (res: EditorSetting) => {
    editorSetting.value = res;
  };

  return {
    getSetting,
    getFontSize,
    getMinimap,
    getWordWrap,
    getRunColor,
    save
  };
});
